home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / graph_alg / _dijkstra.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  2.7 KB  |  68 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _dijkstra.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. /*******************************************************************************
  17. *                                                                              *
  18. *  DIJKSTRA  (single source shortest paths)                                    *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23. #include <LEDA/graph_alg.h>
  24. #include <LEDA/node_pq.h>
  25.  
  26.  
  27. void DIJKSTRA(const graph& G, node s, const edge_array<num_type>& cost,
  28.                                             node_array<num_type>& dist,
  29.                                             node_array<edge>& pred )
  30. { /* 
  31.      computes single source shortest paths from node s for 
  32.      a non-negative network (G,cost), computes for all nodes v:
  33.      a) dist[v] = cost of shortest path from s to v
  34.      b) pred[v] = predecessor edge of v in shortest paths tree
  35.   */
  36.  
  37.   node_pq<num_type>  PQ(G);
  38.   node v;                                                                    
  39.   edge e;                                                                      
  40.                                                                                
  41.   forall_nodes(v,G)                                                            
  42.   { pred[v] = nil;                                                             
  43.     dist[v] = max_num;                                                      
  44.    }                                                                         
  45.  
  46.   dist[s] = 0;
  47.   PQ.insert(s,0);
  48.                                                                                
  49.   while (! PQ.empty())
  50.   { node u = PQ.del_min();
  51.     num_type du = dist[u];
  52.     forall_adj_edges(e,u)                                                    
  53.     { v = target(e);                                                      
  54.       num_type c = du + cost[e];                                              
  55.       if (c < dist[v])                                                    
  56.       { if (dist[v] == max_num) 
  57.            PQ.insert(v,c);
  58.         else 
  59.            PQ.decrease_inf(v,c);
  60.         dist[v] = c;                                                     
  61.         pred[v] = e;                                                     
  62.        }                                                                 
  63.      }                                                                    
  64.    } 
  65. }
  66.  
  67.